import api from '@/api'
import {
  getToken,
  setToken,
  removeToken,
  getRole,
  setRole,
  removeRole,
  getUserName,
  setUserName,
  removeUserName,
  getUserId,
  setUserId,
  removeUserId,
  getUserLogo,
  setUserLogo,
  removeUserLogo,
  getRealName,
  setRealName,
  removeRealName,
  getResourceType,
  removeResourceType
} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    role: getRole(),
    username: getUserName(),
    realName: getRealName(),
    userid: getUserId(),
    userlogo: getUserLogo(),
    resourceType: getResourceType()
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROLE: (state, role) => {
      state.role = role
    },
    SET_USER_NAME: (state, username) => {
      state.username = username
    },
    SET_REALNAME_NAME: (state, realName) => {
      state.realName = realName
    },
    SET_USER_ID: (state, userid) => {
      state.userid = userid
    },
    SET_USER_LOGO: (state, userlogo) => {
      state.userlogo = userlogo
    },
    SET_RESOURCE_TYPE: (state, resourceType) => {
      state.resourceType = resourceType
    }
  },
  actions: {
    // 登录
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        api
          .login(data)
          .then(response => {
            commit('SET_TOKEN', response.access_token)
            setToken(response.access_token)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 获取用户信息(id)
    GetUserInformation({ commit, state }, type) {
      return new Promise((resolve, reject) => {
        const data = { access_token: state.token, type }
        commit('SET_ROLE', type)
        setRole(type)
        api
          .getInformation(data)
          .then(response => {
            if (response.data.isTrue) {
              commit('SET_USER_NAME', response.data.user.username)
              commit('SET_USER_ID', response.data.user.id)
              setUserName(response.data.user.username)
              setUserId(response.data.user.id)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 根据用户id获取用户详细信息(头像)
    getUserDetail({ commit, state }) {
      return new Promise((resolve, reject) => {
        api
          .personalInformation({
            id: state.userid,
            username: state.username
          })
          .then(res => {
            commit('SET_REALNAME_NAME', res.data.user.nickname)
            setRealName(res.data.user.nickname)
            if (+res.code === 2000) {
              let userlogo =
                process.env.BASE_API +
                '/api/vr-business-service' +
                res.data.imageUri +
                (res.data.user.imageId || '')
              commit('SET_USER_LOGO', userlogo)
              setUserLogo(userlogo)
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 退出登录
    Logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        const data = { access_token: state.token }
        api
          .logout(data)
          .then(response => {
            commit('SET_TOKEN', '')
            commit('SET_ROLE', '')
            commit('SET_USER_NAME', '')
            commit('SET_REALNAME_NAME', '')
            commit('SET_USER_ID', '')
            commit('SET_USER_LOGO', '')
            removeToken()
            removeRole()
            removeUserName()
            removeUserId()
            removeUserLogo()
            removeRealName()
            removeResourceType()
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default user
